package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"huxiaopang/models"
)

type StoreController struct {
	ApiBaseController
}

// @router /editpermission [post]
func (this *StoreController) EditPermission() {
	bean := BaseBean{}
	storeCode := this.GetString("store_code")
	if len(storeCode) == 0 {
		bean.Code = "-1"
		bean.Message = "store_code不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	permissionSwitch := this.GetString("permission_switch")
	if len(permissionSwitch) == 0 {
		bean.Code = "-1"
		bean.Message = "permission_switch不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	storeName := this.GetString("store_name")
	if len(storeName) == 0 {
		bean.Code = "-1"
		bean.Message = "store_name不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	storePermissionData := models.StorePermissionData{}
	storePermissionData.StoreCode = storeCode

	if err := storePermissionData.Read("StoreCode"); err != nil && err!=orm.ErrNoRows{
		bean.Code = "-1"
		bean.Message = "系统繁忙"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}else if err == orm.ErrNoRows{
		data := models.StorePermissionData{}
		data.StoreCode = storeCode
		data.TableCodePermission = permissionSwitch
		data.StoreName = storeName
		data.Insert()
	}else {
		storePermissionData.TableCodePermission = permissionSwitch
		storePermissionData.Update("TableCodePermission")
	}

	this.Data["json"] = map[string]interface{}{
		"code": "0",
		"msg":  "SUCCESS",
		"data": "更新成功",
	}
	this.ServeJSON()
	return
}


// @router /readpermission [post]
func (this *StoreController) ReadPermission() {
	bean := BaseBean{}
	storeCode := this.GetString("store_code")
	if len(storeCode) == 0 {
		bean.Code = "-1"
		bean.Message = "store_code不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	storePermissionData := models.StorePermissionData{}
	storePermissionData.StoreCode = storeCode
	var permission string

	if err := storePermissionData.Read("StoreCode"); err != nil && err!=orm.ErrNoRows{
		bean.Code = "-1"
		bean.Message = "系统繁忙"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}else if err == orm.ErrNoRows{
		this.Data["json"] = map[string]interface{}{
		"code": "0",
		"msg":  "SUCCESS",
		"data": "N",
		}
		this.ServeJSON()
		return
	}else {
		permission = storePermissionData.TableCodePermission
	}

	this.Data["json"] = map[string]interface{}{
		"code": "0",
		"msg":  "SUCCESS",
		"data": permission,
	}
	this.ServeJSON()
	return
}

// @router /gettablecodelist [post]
func (this *StoreController) GetTablecodeList() {
	bean := BaseBean{}
	storeCode := this.GetString("store_code")
	if len(storeCode) == 0 {
		bean.Code = "-1"
		bean.Message = "store_code不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	tableCode := this.GetString("table_code")
	var list []TableCodeData
	var tableCodeListsSql string
	o := orm.NewOrm()
	if len(tableCode) == 0 {
		// tableCodeListsSql = fmt.Sprintf("SELECT table_code, link_address from table_number_data WHERE store_code = (SELECT store_code from store_data WHERE store_code = \"%s\" AND table_code_permission = \"%s\")", storeCode, "Y")
		tableCodeListsSql = fmt.Sprintf("SELECT table_number_data.table_code, table_number_data.link_address, device_tablecode_data.device_id from table_number_data LEFT JOIN device_tablecode_data on table_number_data.store_code = device_tablecode_data.store_code AND table_number_data.table_code = device_tablecode_data.table_code  WHERE table_number_data.store_code = (SELECT store_code from store_data WHERE store_code = \"%s\" AND table_code_permission = \"%s\")", storeCode, "Y")
	}else {
		// tableCodeListsSql = fmt.Sprintf("SELECT table_code, link_address from table_number_data WHERE store_code = (SELECT store_code from store_data WHERE store_code = \"%s\" AND table_code_permission = \"%s\") AND table_code = \"%s\"", storeCode, "Y", tableCode)
		tableCodeListsSql = fmt.Sprintf("SELECT table_number_data.table_code, table_number_data.link_address, device_tablecode_data.device_id from table_number_data LEFT JOIN device_tablecode_data on table_number_data.store_code = device_tablecode_data.store_code AND table_number_data.table_code = device_tablecode_data.table_code  WHERE table_number_data.store_code = (SELECT store_code from store_data WHERE store_code = \"%s\" AND table_code_permission = \"%s\") AND table_number_data.table_code = \"%s\"", storeCode, "Y", tableCode)

	}
	beego.Debug(tableCodeListsSql)
	tableCodeListsNum, tableCodeListsErr := o.Raw(tableCodeListsSql).QueryRows(&list)
	if tableCodeListsErr != nil || tableCodeListsNum < 0 {
		bean.Code = "-1"
		bean.Message = "查询失败"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}
	beego.Debug(list)
	this.Data["json"] = map[string]interface{}{
		"code": "0",
		"msg":  "SUCCESS",
		"data": list,
	}
	this.ServeJSON()
	return

}